package com.ruoyi.pcwl.controller;

import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.pcwl.domain.Article;
import com.ruoyi.pcwl.domain.vo.Result;
import com.ruoyi.pcwl.service.IArticleContentService;
import com.ruoyi.pcwl.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章信息，存储已发布的文章Controller
 *
 * @author ruoyi
 * @date 2024-01-11
 */
@Api(tags = "文章管理接口")
@RestController
@RequestMapping("/pcwl/article")
public class ArticleController extends BaseController
{
    @Autowired
    private IArticleService articleService;

    /**
     * 发布文章到小程序
     */
    @ApiOperation("发布文章到小程序")
//    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @PutMapping("/publishArticle/{id}")
    public AjaxResult publishArticle(@PathVariable("id") String id) throws WxErrorException {
        articleService.publishArticle(id);
        return success();
    }

    /**
     * 同步微信文章
     */
    @ApiOperation("同步微信文章")
//    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/syncArticle")
    public AjaxResult syncArticle() throws WxErrorException {
        articleService.syncWechatArticle();
        return success();
    }

    /**
     * 查询文章信息，存储已发布的文章列表
     */
    @ApiOperation("分页获取文章列表")
//    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(Article article)
    {
        startPage();
        List<Article> list = articleService.selectArticleList(article);
        return getDataTable(list);
    }

    /**
     * 导出文章信息，存储已发布的文章列表
     */
//    @PreAuthorize("@ss.hasPermi('system:article:export')")
    @Log(title = "文章信息，存储已发布的文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Article article)
    {
        List<Article> list = articleService.selectArticleList(article);
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
        util.exportExcel(response, list, "文章信息，存储已发布的文章数据");
    }

    /**
     * 获取文章信息，存储已发布的文章详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(articleService.selectArticleById(id));
    }

    /**
     * 根据Id查询对象
     */
    @ApiOperation("根据Id查询对象")
    @GetMapping("/getArticleById/{id}")
    public Result getArticleById(@PathVariable Long id) {
        return Result.ok(articleService.getArticleById(id));
    }

    /**
     * 新增文章信息，存储已发布的文章
     */
//    @PreAuthorize("@ss.hasPermi('system:article:add')")
    @Log(title = "文章信息，存储已发布的文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Article article)
    {
        return toAjax(articleService.insertArticle(article));
    }

    /**
     * 修改文章信息，存储已发布的文章
     */
//    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "文章信息，存储已发布的文章", businessType = BusinessType.UPDATE)
    @PutMapping("/updateArticleById/{id}")
    public AjaxResult edit(@RequestBody Article article,@PathVariable Long id)
    {
        return toAjax(articleService.updateArticle(article));
    }

    @Resource
    IArticleContentService articleContentService;
    /**
     * 删除文章信息，存储已发布的文章
     */
//    @PreAuthorize("@ss.hasPermi('system:article:remove')")
//    @Log(title = "文章信息，存储已发布的文章", businessType = BusinessType.DELETE)
    @ApiOperation("删除文章,会一并删除公众号中的文章,请谨慎操作")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        articleService.deleteArticleByIds(ids);
        articleService.removeBatchByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    public TableDataInfo getDataTable(IPage page)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }
}
